/**
 * Created by CHENCO7 on 9/1/2017.
 */
require('../models/order.model');
require('../models/customer.model');
const mongoose = require('mongoose');
let orderModel = mongoose.model('order');

module.exports = {
    updateOrderStatusById: updateOrderStatusById,
    findOrdersByMerchantId: findOrdersByMerchantId,
    findOrdersByMerchantIdByStatus:findOrdersByMerchantIdByStatus
};

function findOrdersByMerchantId(merchantId,callback) {
    orderModel.find({merchant:merchantId})
        .populate({path:'customer',select:'name address'})
        .exec(function(err, data){
        callback(err, data);
    })
}

function findOrdersByMerchantIdByStatus(merchantId,status,callback){
    orderModel.find({merchant:merchantId,status:status})
        .populate({path:'customer',select:'name address'})
        .exec(function(err, data){
            callback(err, data);
        })
}

function updateOrderStatusById(order,merchantId, callback) {
    orderModel.update({_id:order.id,merchant:merchantId},{$set:{status:order.status}}, function (err, data) {
        callback(err, data);
    })
}
